package com.getpebble.android.common.core.trace;

import android.content.Context;
import android.database.ContentObserver;
import com.getpebble.android.common.core.async.PblAsyncTask;
import com.getpebble.android.common.core.util.PblContentProviderUtil;
import com.getpebble.android.common.model.PblDeviceModel;
import com.getpebble.android.common.model.PblDeviceModel.PblDeviceRecord;
import com.getpebble.android.common.model.PblDeviceModel.Query;
import java.lang.ref.WeakReference;
import java.util.List;
import java.util.Map;

class PebbleDeviceObserver extends ContentObserver {
    PblDeviceRecord mActive;
    WeakReference<Context> mContext;

    public PebbleDeviceObserver(Context context) {
        super(null);
        this.mContext = new WeakReference(context);
        useRecord(null);
        register(context);
        useRecord(findActiveRecord());
    }

    public void register(Context context) {
        context.getContentResolver().registerContentObserver(PblContentProviderUtil.getTableUri("devices"), true, this);
    }

    public void onChange(boolean selfChange) {
        new PblAsyncTask() {
            public boolean doInBackground() {
                PebbleDeviceObserver.this.useRecord(PebbleDeviceObserver.this.findActiveRecord());
                return true;
            }

            public void onTaskSuccess() {
            }

            public void onTaskFailed() {
            }
        }.submit();
    }

    private PblDeviceRecord findActiveRecord() {
        Context context = (Context) this.mContext.get();
        if (!(context == null || context.getContentResolver() == null)) {
            List<PblDeviceRecord> connectedRecords = PblDeviceModel.getPblDeviceRecords(context.getContentResolver(), Query.CONNECTION_STATUS_CONNECTED);
            if (connectedRecords != null && connectedRecords.size() > 0) {
                return (PblDeviceRecord) connectedRecords.get(0);
            }
        }
        return null;
    }

    public void unregister() {
        Context context = (Context) this.mContext.get();
        if (context != null) {
            context.getContentResolver().unregisterContentObserver(this);
        }
    }

    private void useRecord(PblDeviceRecord newRecord) {
        this.mActive = newRecord;
        synchronized (AnalyticsLogger.class) {
            Map<String, Object> event = AnalyticsLogger.getStaticGlobalProperties();
            Map<String, Object> remoteDevice = AnalyticsLogger.getOrCreateChildMap(AnalyticsLogger.getOrCreateChildMap(event, "device"), "remote_device");
            remoteDevice.put("bt_address", formatBtAddress(this.mActive != null ? this.mActive.pblDevice.getAddress() : ""));
            Map<String, Object> firmware = AnalyticsLogger.getOrCreateChildMap(AnalyticsLogger.getOrCreateChildMap(AnalyticsLogger.getOrCreateChildMap(remoteDevice, "firmware_description"), "version"), "firmware");
            firmware.put("fw_version", this.mActive != null ? this.mActive.fwVersion.toString() : "");
            String recoveryFwVersion = "";
            if (!(this.mActive == null || this.mActive.recoveryFwVersion == null)) {
                recoveryFwVersion = this.mActive.recoveryFwVersion.toString();
            }
            firmware.put("recovery_fw_version", recoveryFwVersion);
            remoteDevice.put("hw_version", this.mActive != null ? this.mActive.hwRevision : "");
            String serialNumber = this.mActive != null ? this.mActive.serialNumber : "";
            remoteDevice.put("serial_number", serialNumber);
            remoteDevice.put("type", "watch");
            AnalyticsLogger.getOrCreateChildMap(event, "identity").put("serial_number", serialNumber);
            AnalyticsLogger.setStaticGlobalProperties();
        }
    }

    private String formatBtAddress(String rawAddress) {
        StringBuilder address = new StringBuilder();
        for (char c : rawAddress.toCharArray()) {
            if (c != ':') {
                address.append(Character.toUpperCase(c));
            }
        }
        return address.toString();
    }
}
